package com.wh.wisdomsite.xbox.client.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wh.wisdomsite.xbox.ba.entity.Role;
import com.wh.wisdomsite.xbox.client.session.ConfigClientSessionUser;
import com.wh.wisdomsite.xbox.common.AbstractXboxRequest;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.core.session.impl.SimpleSession;
import com.wh.wisdomsite.xbox.us.entity.Company;
import com.wh.wisdomsite.xbox.us.entity.User;
import org.apache.commons.lang3.StringUtils;

/**
 * 配置端用户工具类
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.client.utils
 * @author: 丁奕
 * @date: 2018-11-06 10:07
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
public class ConfigUserUtils {
    /**
     * 获取当前的配置端会话用户
     * @param in
     * @return
     */
    public static ConfigClientSessionUser getCclSessionUser(AbstractXboxRequest in){
        JSONObject jsonObject = (JSONObject)((SimpleSession)in.getXboxRequestContext().getSession()).getAttribute(Constants.CONFIG_SESSION_USER);
        ConfigClientSessionUser configClientSessionUser = JSONObject.toJavaObject(jsonObject, ConfigClientSessionUser.class);
        return configClientSessionUser;
    }

    /**
     * 获取当前登录的用户
     * @param in
     * @return
     */
    public static User getCurrentUser(AbstractXboxRequest in){
        User user = getCclSessionUser(in).getUser();
        return user;
    }

    /**
     * 获取当前登录的用户的总公司
     * @param in
     * @return
     */
    public static Company getCurrentUserParentCompany(AbstractXboxRequest in){
        Company company = getCclSessionUser(in).getHeadCompany();
        return company;
    }

    /**
     * 当前登录用户是否超级管理员
     * @return
     */
    public static boolean isSysAdmin(AbstractXboxRequest in){
        for(Role role : getCclSessionUser(in).getRoleList()){
            if(role != null && (role.getRoId().equals("0") || role.getRoId().equals("2"))){
                return true;
            }
        }
        return false;
    }

    /**
     * 在用户不是超级管理员的时候根据用户的数据权限设置查找范围
     * @param in
     * @param ew
     * @param companyTableAlias
     * @param officeTableAlias
     * @param projectTableAlias
     * @return
     */
    public static EntityWrapper setEntityWrapper(AbstractXboxRequest in, EntityWrapper ew, String companyTableAlias,
                                                 String officeTableAlias, String projectTableAlias) {
        ConfigClientSessionUser cclSessionUser = getCclSessionUser(in);
        String dataScope = cclSessionUser.getDataScope();
        //设置一个查询用的代码
        String codeForFind = null;
        switch (dataScope) {
            case "1":
                codeForFind = cclSessionUser.getFinalCompCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.like(companyTableAlias + ".compCode", codeForFind);
                }
                break;
            case "2":
                codeForFind = cclSessionUser.getFinalCompCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.eq(companyTableAlias + ".compCode", codeForFind);
                }
                break;
            case "3":
                codeForFind = cclSessionUser.getEmployee().getOffice().getOfficeCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.like(officeTableAlias +".officeCode", codeForFind);
                }
                break;
            case "4":
                codeForFind = cclSessionUser.getEmployee().getOffice().getOfficeCode();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.eq(officeTableAlias + ".officeCode", codeForFind);
                }
                break;
            case "5":
                codeForFind = cclSessionUser.getEmployee().getPrFinished() + "," + cclSessionUser.getEmployee().getPrId();
                if(StringUtils.isNotEmpty(codeForFind)){
                    ew.in(projectTableAlias + ".prId", codeForFind);
                }
                break;
            default:break;
        }
        return ew;
    }
}
